#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-10-12 9:13 上午
# @Author  : yangdy
# @File    : 2020-10-12.530.二叉搜索树的最小绝对差
# @Software: PyCharmCE
from tools.my_methods import *


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def getMinimumDifference(self, root: TreeNode) -> int:
        self.val, self.ans = -1, -1

        def dfs(rt):
            if not rt:
                return
            dfs(rt.left)
            if self.val >= 0 and (self.ans == -1 or self.ans > rt.val - self.val):
                self.ans = rt.val - self.val
            self.val = rt.val
            dfs(rt.right)
        dfs(root)
        return self.ans
